package com.foreknow.demo7;

/**
 * 主人类
 */
public class Master {

    public void cure(Pet pet){//pet = Dog对象
        Dog d = (Dog) pet;//如果将大类型(父类型)传递给小类型(子类型)》》引用类型的强制类型转换(向下造型)
        if (pet.getHealth() < 60){
           pet.toHospital();
        }
    }
    public void feed(Pet pet){
        pet.eat();
    }

    public void play(Pet pet) {
        if (pet instanceof Dog){
            Dog dog =(Dog) pet;//如果要调用子类中的新方法，需要强制类型转换
            dog.game();
        }else if (pet instanceof Penguin){
            Penguin penguin = (Penguin) pet;
            penguin.swimming();
        }
    }

    public static void main(String[] args) {
        Master master = new Master();
        Pet pet = new Penguin();
        master.feed(pet);
    }
}
